<?php

class CommonAction extends Action {
	
    /**
     * 权限认证初始化
     * */
	function _initialize() {
        // 用户权限检查
		if (!isset($_SESSION [C ( 'USER_AUTH_KEY' )]) ) {
			//跳转到认证网关
			redirect ( PHP_FILE . C ( 'USER_AUTH_GATEWAY' ) );
		}
	}
    
    /**
     * 进入添加页面
     * 对应添加功能为insert
     * */
    function add(){
        $this->display();
    }
    
    /**
     * 正式添加数据
     * */
    function insert(){
        $name=$this->getActionName();
		$model = D ($name);
		if (false === $model->create ()) {
			$this->errorAjax( $model->getError () );
		}
		//保存当前数据对象
		$list=$model->add ();
		if ($list!==false) { //保存成功
			$this->successAjax('新增成功!');
		} else {
			//失败提示
			$this->errorAjax('新增失败!');
		}
    }
    
    /**
     * 进入编辑页面
     * 对应编辑功能为update
     * */
    function edit(){
        $name=$this->getActionName();
		$model = M ( $name );
		$id = $_REQUEST [$model->getPk ()];
		$vo = $model->getById ( $id );
		$this->assign ( 'vo', $vo );
		$this->display ();
    }
    
    /**
     * 更新记录
     * */
    function update(){
        $name=$this->getActionName();
		$model = D ( $name );
		if (false === $model->create ()) {
			$this->errorAjax ( $model->getError () );
		}
		// 更新数据
		$list=$model->save ();
		if (false !== $list) {
			//成功提示
			$this->successAjax ('编辑成功!');
		} else {
			//错误提示
			$this->errorAjax ('编辑失败!');
		}
    }
    
    
    /**
     * （单）永久删除
     * */
    public function delete(){
        //删除指定记录
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$pk = $model->getPk ();
			$id = $_REQUEST [$pk]; //id
			if (isset ( $id )) {
				$condition[$pk] = $id;
				if (false !== $model->where ( $condition )->delete ()) {
					//echo $model->getlastsql();
					$this->successAjax ('删除成功！');
				} else {
					$this->errorAjax ('删除失败！');
				}
			} else {
				$this->errorAjax ( '非法操作01' );
			}
		}
		$this->errorAjax ( '非法操作02' );
    }
    
    /**
     * （批量）永久删除数据
     * */
    public function moredelete() {
		//删除指定记录
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$pk = $model->getPk ();
			$id = $_REQUEST [$pk.'s']; //ids
			if (isset ( $id )) {
				$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
				if (false !== $model->where ( $condition )->delete ()) {
					//echo $model->getlastsql();
					$this->successAjax ('删除成功！');
				} else {
					$this->errorAjax ('删除失败！');
				}
			} else {
				$this->errorAjax ( '非法操作01' );
			}
		}
		$this->errorAjax ( '非法操作02' );
	}
    

    
    
    /**
     * dwz Ajax数据返回
     * success返回
     * */
    function successAjax($message ,$isCloseCurrent = false){
        $result  =  array();
        $result['statusCode']  =  1;	// ok:1
        $result['navTabId']  =  $_REQUEST['navTabId'];	// 刷新页面
        if($isCloseCurrent){
            $result['callbackType'] = 'closeCurrent';
        }else{
            $result['callbackType'] = '';
        }
        $result['message'] =  $message;
        echo json_encode($result);
        exit();
    }
    
    /**
     * dwz Ajax数据返回
     * error返回
     * */
    function errorAjax($message ,$isCloseCurrent = false){
        $result  =  array();
        $result['statusCode']  =  0;	// error:0
        $result['navTabId']  =  $_REQUEST['navTabId'];	// 刷新页面
        if($isCloseCurrent){
            $result['callbackType'] = 'closeCurrent';
        }else{
            $result['callbackType'] = '';
        }
        $result['message'] =  $message;
        echo json_encode($result);
        exit();
    }
}